﻿using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Data.Entity;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using EKAABSInformationManagement.Service;
using EKAABSInformationManagement.Models;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Controllers
{
    public class BellAccountController : Controller
    {
        public BellAccount accountService { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (System.Web.HttpContext.Current.User.IsInRole("admin") != true)
            {
                HttpException ex = new HttpException(401, "");
                requestContext.HttpContext.AddError(ex);
            }
            if (accountService == null) { accountService = new BellAccount(); }
            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            List<accountInBell> accounts = accountService.getAllBellAccount();
            return View(accounts);
        }

        [HttpPost]
        public ActionResult Create(accountInBell account)
        {
            try
            {
                bool isExists = default(bool);
                accountService.create(account, out isExists);
                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult Edit(accountInBell account)
        {
            try
            {
                bool isExists = default(bool);
                accountService.edit(account, out isExists);
                if (isExists != false)
                {
                    return Json(new { status = 1 });
                }
                else
                {
                    return Json(new { status = 2 });
                }
            }
            catch { return Json(new { status = 4 }); }
        }

        [HttpPost]
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                accountService.delete(strs);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
        [HttpPost]
        public ActionResult batchUpdate(string ids,accountInBell a)
        {
            try
            {
                string[] strs = ids.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                accountService.batchUpdate(strs, a);
                return Json(new { status = 2 });
            }
            catch { return Json(new { status = 4 }); }
        }
    }
}
